import glob
import astro.fit
reload(astro.fit)
import astro.spec
import astro.io
import os
import sys

def main(filename, z):
    z = float(z)
    newfilename = filename + '.cont'
    if os.path.lexists(newfilename):
        print '%s already exists' % newfilename
        sys.exit(1)
    print 'Redshift', z
    sp = astro.spec.read(filename)
    co, cpts = astro.fit.fitqsocont(
        sp.wa, sp.fl, sp.er, z, nbin=1,divmult_forest=None, divmult=1) 
    sp.co = co
    sp.write(newfilename)

if __name__ == '__main__':
    if len(sys.argv[1:]) != 2:
        print 'Usage: fitcont.py [spectrum] [redshift]'
        sys.exit(1)
    main(*sys.argv[1:])
